public class InitiateRenewalOrderFromAccount{
    private final account Acct;
    public account acc;
    public List<Contact> cont = new List<Contact>();
    public string account_type;
    //public Notes__c newNote ;

    ID localAdvertiserRecordType1ID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'SFA-OOR Renewals');
    
        public InitiateRenewalOrderFromAccount(ApexPages.StandardController stdController) {
            Id Acctid = ApexPages.currentPage().getParameters().get('accid');        
            account_type = ApexPages.currentPage().getParameters().get('atype');       
            
            //this.acct = (Account)stdController.getRecord();
            system.debug('LLLLLLLLLL' + Acctid);
            acc = [select id,ownerid,name,Hot_Renewal__c,Renewal_Status__c,SFA_OOR_Payment_Type__c,Projected_Close_Date__c,Renewal_Type__c,Secondary_owner__c,product_family__c,Days_Past_due__c, Projected_Revenue_mo__c,Amount_Due__c,Recurring_Charge__c,Onetime_Charge_OC__c,Contract_Status__c,Renewal_Date__c,Cancel_Reason__c,Will_Cancel_on__c,SFA_OOR_Future_Cancel_Date__c,Search_Product_Count__c,Number_Of_Active_Contracts__c,Payment_Type__c,Contract_Signature_Date__c from account where id = :Acctid];
            cont = [select id from contact where accountid = :acc.id and primary_indicator__c = true limit 1]; }
         
        
         public PageReference CreateRenewalOrder(){
             Opportunity opty = new Opportunity();
             Opty.Name = acc.Name;
             Opty.Accountid = acc.id;
             
             if(acc.Projected_Close_Date__c != null){
                 opty.Projected_close_date__c = acc.Projected_Close_Date__c;}
             else{
                 opty.Projected_close_date__c = system.today();
                 }
             
             if(account_type=='Cancel Order'){
             
             system.debug('I am inside Cancel Order'+account_type);
             opty.Opportunity_Type__c='Cancel Customer';
             opty.Revenue_mo__c=0;
             opty.Opportunity_Amount__c =0;
             }
             else{
             opty.Opportunity_Type__c = acc.Renewal_Type__c;
             opty.Opportunity_Amount__c = acc.Projected_Revenue_mo__c;
             opty.Revenue_mo__c = acc.Projected_Revenue_mo__c;
             opty.Secondary_owner__c =  Acc.Secondary_owner__c;
             }
             
             opty.CloseDate = system.today();
             opty.StageName = 'Signed Contract';
             opty.product_family__c = acc.product_family__c;
             opty.Amount_Due__c = acc.Amount_Due__c;
             opty.Recurring_Charge__c = acc.Recurring_Charge__c;
             opty.Onetime_Charge_OC__c = acc.Onetime_Charge_OC__c;
             opty.Contract_Status__c = acc.Contract_Status__c;
             opty.Client_Renewal_Contact_Date__c = acc.Renewal_Date__c;
             opty.Cancel_Reason__c = acc.Cancel_Reason__c;
             opty.Will_Cancel_on__c = acc.Will_Cancel_on__c;
             opty.Future_Cancel_Date__c = acc.SFA_OOR_Future_Cancel_Date__c;
             opty.Search_Product_Count__c = acc.Search_Product_Count__c;
             opty.Number_Of_Active_Contracts__c = acc.Number_Of_Active_Contracts__c;
             opty.Days_Past_due__c = acc.Days_Past_due__c;
             opty.Payment_Type__c = acc.SFA_OOR_Payment_Type__c;
             opty.Contract_Signature_Date__c = acc.Contract_Signature_Date__c;
             opty.recordtypeid = localAdvertiserRecordType1ID;
             opty.ownerId = acc.ownerId;
             if(cont.size() > 0){
             opty.Contact_name__c = cont[0].id;}
             opty.lead_source_id__c = 'EC'; 
             Insert opty;
             //clearing all renewal fields on account after order has been initiated
             acc.Renewal_Type__c = null;
             acc.Projected_Close_Date__c =null;
             acc.Projected_Revenue_mo__c=null;
             acc.Secondary_Owner__c = null;
             acc.Renewal_Status__c = 'Order Initiated';
             acc.Hot_Renewal__c = false;
             acc.Product_Family__c=null;
             update acc;
             
             //create a corresponding note for the account
             Notes__c newNote = new NOtes__c();
             newNote.Type__c = 'Renewal';
             newNote.Notes__c = 'A New Renewal Order has been initiated';
             newNote.Account__c = acc.id;
             newNote.disposition__c = 'Order Initiated';
             insert newNote;
             
             PageReference next = new PageReference('/'+ opty.id);
             next.setRedirect(true);
             return next;
         }
           
            //Venkat added this test method
            
        static testMethod void testInitiateRenewalOrderFromAccount()
             {
                   
            
            /* created an opportunity to cover the code for Create Notes */
            Account accObj = new Account(Name='Test Account');
            accObj.type__c = 'Prospect'; 
            accObj.Projected_Close_Date__c = System.today();
            insert accObj;
            // Contact insert to cover the code for the trigger 
            
            Contact cnt = new Contact();
            cnt.IsPrimary_Contact__c=true;
            cnt.AccountId=accObj.Id;
            //cnt.RecordTypeId=localContactRecID;
            cnt.FirstName = 'Krish';
            cnt.LastName='Suni';
            cnt.Phone='5555555';
            
            insert cnt;
            
             Opportunity opp = new Opportunity();
            opp.AccountId= accObj.Id;
            opp.Name='Test Opp';
            opp.LeadSource='Internet';
            Date curr = Date.today();
            opp.CloseDate=curr;
            opp.Projected_close_date__c = curr;
            opp.StageName='New';
            opp.Most_Recent_Notes__c='testcoverage';
            insert opp;
            
            ApexPages.StandardController thecontroller = new ApexPages.StandardController(accObj);
            
            
            PageReference pg = new PageReference('/apex/ConvertLead');
            pg.getParameters().put('accid',accObj.id);
            
            Test.setCurrentPageReference(pg);
            
            InitiateRenewalOrderFromAccount controller = new InitiateRenewalOrderFromAccount(thecontroller);
            controller.CreateRenewalOrder();
                            
             }     
            
}